package com.cgbl.cg.ui.home

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ImgAdapter
import com.cgbl.cg.adapter.LabelAdapter
import com.cgbl.cg.databinding.AppActivityCaseDetailsBinding
import com.cgbl.cg.vm.home.CaseDetailsViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants

@Route(path = AppConstants.Router.HOME.A_CASE_DETAILS)
class CaseDetailsActivity : GLBaseActivity<AppActivityCaseDetailsBinding, CaseDetailsViewModel>() {

    private lateinit var imgAdapter: ImgAdapter
    private lateinit var tagAdapter: LabelAdapter
    var id = ""

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_case_details

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_en_tips_72))
        initAdapter()
    }

    override fun initParam() {
        super.initParam()
        id = intent.getStringExtra("id") ?: ""
    }

    override fun initData() {
        super.initData()
        viewModel.getCase(id)
    }

    private fun initAdapter() {
        imgAdapter = ImgAdapter()
        binding.recyclerViewImg.apply {
            adapter = imgAdapter
        }

        tagAdapter = LabelAdapter()
        binding.recyclerViewTag.apply {
            adapter = tagAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.labelDataEvent.observe(this) {
            it?:return@observe
            tagAdapter.setNewInstance(it.split(",").toMutableList())
        }

        viewModel.uc.imgDataEvent.observe(this) {
            it.mapNotNull { it.filePath }.let { it1 ->
                imgAdapter.setNewInstance(it1.toMutableList())
            }

        }
    }
}